package cn.edu.ctbu.labtest.service.impl;

import cn.edu.ctbu.labtest.dao.TeacherRepository;
import cn.edu.ctbu.labtest.domain.Teacher;
import cn.edu.ctbu.labtest.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherImplService implements TeacherService {
    @Autowired
    TeacherRepository teacherRepository;

    /**
     * 读取所有的学生写数据
     * @return
     */
    public List<Teacher> findAll(){

        return teacherRepository.findAll();

    }
    /**
     * 只支持分页的查询
     * @param pageable
     * @return
     */
    public Page<Teacher> findAll(Pageable pageable){
        return teacherRepository.findAll(pageable);
    };

    public List<Teacher> findByNameAndPassword(String name, String password){
        return teacherRepository.findByNameAndPassword(name, password);
    }

    /**
     *
     * @param pageable
     * @return
     */
    public Page<Teacher> findAll(Example<Teacher> teacher,Pageable pageable){
      return teacherRepository.findAll(teacher,pageable);
    };

    /*插入学生*/
    public Teacher insert(Teacher teacher){
        return teacherRepository.save(teacher);
    }

    /*更新学生*/
    public Teacher update(Teacher teacher){
        return teacherRepository.save(teacher);
    }

    /*通过Id获取学生*/
    public Teacher getById(Integer id){
        Teacher teacher = teacherRepository.findById(id).orElse(null);
        return teacher;
    }

    /*通过姓名查询学生*/
    public List<Teacher> findByName(String name){
        return teacherRepository.findByName(name);
    }

    /**
     * 删除学生
     */
    public void delete(Teacher teacher){

        teacherRepository.delete(teacher);
    }

    public void delete(Integer id){

        Teacher teacher = new Teacher();
        teacher.setId(id);

        teacherRepository.delete(teacher);

    }
}

